package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.Achievement;
import com.interagriculture.entity.query.AchievementQuery;
import com.interagriculture.mapper.AchievementMapper;
import com.interagriculture.service.AchievementService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Achievement)表服务实现类
 *
 * @author makejava
 * @since 2024-10-24 19:14:25
 */
@Service("achievementService")
public class AchievementServiceImpl implements AchievementService {
    @Resource
    private AchievementMapper achievementMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public Achievement queryById(Integer userId) {
        return this.achievementMapper.queryById(userId);
    }




    /**
     * 新增数据
     *
     * @param achievement 实例对象
     * @return 实例对象
     */
    @Override
    public Achievement insert(Achievement achievement) {
        this.achievementMapper.insert(achievement);
        return achievement;
    }

    /**
     * 修改数据
     *
     * @param achievement 实例对象
     * @return 实例对象
     */
    @Override
    public Achievement update(Achievement achievement) {
        this.achievementMapper.update(achievement);
        return this.queryById(achievement.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.achievementMapper.deleteById(userId) > 0;
    }

    /**
     * 根据条件分页查询
     * @param achievementQuery
     * @return
     */
    @Override
    public PageResult selectByPage(AchievementQuery achievementQuery) {
        if(achievementQuery != null&& achievementQuery.getPageNum()!=null && achievementQuery.getPageSize()!=null){
            PageHelper.startPage(achievementQuery.getPageNum(),achievementQuery.getPageSize());
        }
        if(achievementQuery!=null && achievementQuery.getOrderBy()!=null){
            PageHelper.orderBy(achievementQuery.getOrderBy());
        }else {
            //默认根据id倒序排列
            PageHelper.orderBy("id desc");
        }
        List<Achievement> achievementList =achievementMapper.selectByPage(achievementQuery);
        long count = achievementMapper.count(achievementQuery);
        PageResult pageResult = new PageResult(count, achievementList);
        return pageResult;
    }

    /**
     * 添加或更新
     * @param achievement
     */
    @Override
    public void saveOrUpdateInfo(Achievement achievement) {
        if(achievement.getId()==null){
            achievementMapper.insert(achievement);
        }else {
            achievementMapper.update(achievement);
        }

    }
}
